from typing import List, Dict
from .cards.base_card import Card

class Player:
    def __init__(self, character_class: str):
        self.character_class = character_class
        self.stats = self._initialize_stats()
        self.deck = []  # 初始化为空列表
        self.hand = []
        self.discard_pile = []
        self.effects = []
    
    def _initialize_stats(self) -> Dict:
        return {
            "max_hp": 100,
            "current_hp": 100,
            "max_mana": self._get_starting_mana(),
            "current_mana": self._get_starting_mana(),
            "block": 0,
            "gold": 0
        }
    
    def _get_starting_mana(self) -> int:
        """根据角色类型获取初始法力值"""
        return {
            "warrior": 3,
            "mage": 5
        }.get(self.character_class, 4)
    
    def add_effect(self, effect):
        self.effects.append(effect)
    
    def draw_card(self, count: int = 1) -> None:
        """从牌组抽取卡牌"""
        for _ in range(count):
            if not self.deck and self.discard_pile:
                self.shuffle_discard_to_deck()
            if self.deck:
                card = self.deck.pop()
                self.hand.append(card)
    
    def shuffle_discard_to_deck(self) -> None:
        """将弃牌堆洗牌并放入牌组"""
        import random
        self.deck.extend(self.discard_pile)
        random.shuffle(self.deck)
        self.discard_pile.clear()
    
    def add_gold(self, amount: int) -> None:
        """添加金币"""
        self.stats["gold"] += amount
    
    def sort_hand(self) -> None:
        """对手牌进行排序"""
        # 定义卡牌类型的优先级
        type_priority = {
            "attack": 1,
            "defense": 2,
            "skill": 3
        }
        
        # 根据卡牌类型和名称排序
        self.hand.sort(key=lambda card: (type_priority[card.card_type], card.name))
    
    def get_defense(self) -> int:
        """获取当前防御值"""
        return self.stats["block"]
    
    def end_turn(self) -> None:
        """回合结束时重置某些状态"""
        self.stats["block"] = 0  # 格挡值在回合结束时清零
        self.stats["current_mana"] = self.stats["max_mana"]  # 恢复法力值
        
        # 将剩余手牌放入弃牌堆
        self.discard_pile.extend(self.hand)
        self.hand.clear()
        
        # 抽新的手牌
        self.draw_card(5) 